package com.usian.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.usian.yygh.model.hosp.Department;
import com.usian.yygh.repository.DepartmentRepository;
import com.usian.yygh.service.DepartmentService;
import com.usian.yygh.vo.hosp.DepartmentQueryVo;
import com.usian.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author:WGG
 * @Date 2023年01月08日 21:58
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 上传科室信息
     * @param map
     * @return
     */
    @Override
    public void save(Map<String, Object> map) {
        //将map集合转换成Department对象
        String str = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(str, Department.class);
        //判断信息是否存在
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (departmentExist!=null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    /**
     * 查询科室信息
     *
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        //创建pageable对象，设置当前页和每页记录数
        //0是第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 删除科室信息
     * @param map
     * @return
     */
    @Override
    public void remove(Map<String, Object> map) {
        //将map集合转换成Department对象
        String str = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(str, Department.class);
        //查询数据是否存在
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (departmentExist!=null){
            //根据id删除
            departmentRepository.deleteById(departmentExist.getId());
        }
    }

    /**
     * 查询医院所有科室列表
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //定义一个最终集合
        List<DepartmentVo> result = new ArrayList<>();
        //根据hoscode在MongoDB查出科室数据
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> all = departmentRepository.findAll(example);
        //使用stream流进行分组
        Map<String, List<Department>> listMap = all.stream()
                .collect(Collectors.groupingBy(Department::getBigcode));

        //遍历集合
        for (Map.Entry<String, List<Department>> entry : listMap.entrySet()) {
            //获取key 大科室编号
            String bigcode = entry.getKey();
            //获取value 大科室编号对应的数据
            List<Department> value = entry.getValue();

            //创建封装数据的对象
            DepartmentVo departmentVo = new DepartmentVo();
            //封装数据
            //封装大科室
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(value.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department1 : value) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department1.getDepcode());
                departmentVo1.setDepname(department1.getDepname());
                children.add(departmentVo1);
            }
            departmentVo.setChildren(children);
            result.add(departmentVo);
        }
        return result;
    }

    /**
     * 根据科室编号，和医院编号，查询科室名称
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public Object getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }
}
